package com.ruanmou.vip.rabbitmq.demo.topic;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.DeliverCallback;
import com.ruanmou.vip.rabbitmq.demo.route.PBCConfig;
import com.ruanmou.vip.rabbitmq.demo.util.RabbitMQUtil;

/**
 * 欢迎同学们来到牧码人教育
 *
 * @Classname RouteReceiver1
 * @Description TODO
 * @Date 2020-3-7 14:54
 * @Created by Gerry
 */
public class TopicReceiver2 implements TopicConfig {
    public static void main(String[] args) throws Exception {
        Channel channel = RabbitMQUtil.getConnection().createChannel();
        channel.queueDeclare(STUDENT_QUEUE, false, false, false, null);
        channel.exchangeDeclare(EXCHANGE_NAME, TOPIC_TYPE);
        // 将队列绑定到交换机 [routing_key={insert}]
        channel.queueBind(STUDENT_QUEUE, EXCHANGE_NAME,"user.#");
        // 设置公平分发
        channel.basicQos(1);
        DeliverCallback callback = (tag, message) -> {
            String msg = new String(message.getBody(), "utf-8");
            System.out.println("STU_收到消息:" + msg);
            channel.basicAck(message.getEnvelope().getDeliveryTag(), false);
        };
        channel.basicConsume(STUDENT_QUEUE, false, callback, tag->{});
    }
}
